﻿<%@ WebHandler Language="VB" Class="SearchEditCustomer" %>

Imports System
Imports System.Web
Imports System.Collections.Generic
Imports System.Linq

Public Class SearchEditCustomer : Implements IHttpHandler
    Dim Req As String
    Dim Page As Integer
    Dim Rows As Integer
    Dim Sidx As String
    Dim Sord As String
    Dim SearchBy As String
    Dim SearchStr As String
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Req = context.Request("req")
        Page = context.Request("page")
        Rows = context.Request("rows")
        Sidx = context.Request("sidx")
        Sord = context.Request("sord")
        SearchBy = context.Request("searchby")
        SearchStr = context.Request("searchstr")
        
        getTable(context)
    End Sub
    Public Sub getTable(ByVal cont As HttpContext)
        
        Dim SQLDB As New OneCall.DataModel.OneCDataEntities
        Dim CustList As List(Of OneCall.DataModel.Customer)
        If SearchBy = "Email" Then
            CustList = (From p In SQLDB.Customers Where p.Customer_EmailID.Contains(SearchStr) Select p).ToList()
        ElseIf SearchBy = "Mobile" Then
            CustList = (From p In SQLDB.Customers Where p.Customer_Mobile.Contains(SearchStr) Select p).ToList()
        ElseIf SearchBy = "LastName" Then
            CustList = (From p In SQLDB.Customers Where p.Customer_LastName.Contains(SearchStr) Select p).ToList()
        ElseIf SearchBy = "FirstName" Then
            CustList = (From p In SQLDB.Customers Where p.Customer_FirstName.Contains(SearchStr) Select p).ToList()
        Else
            CustList = (From p In SQLDB.Customers Select p).ToList()
        End If
                
        Dim ret As String = "<?xml version='1.0' encoding=""utf-8""?>"
        ret = ret & "<rows>"
        ret = ret & "<page>1</page>"
        ret = ret & "<total>1</total>"
        ret = ret & "<records>" & CustList.Count & "</records>"
        
        For Each Customer As OneCall.DataModel.Customer In CustList
            ret = ret & "<row>"
            ret = ret & "<cell>" & Customer.Customer_ID & "</cell>"
            ret = ret & "<cell><![CDATA[ " & Customer.Customer_EmailID & "]]></cell>"
            ret = ret & "<cell><![CDATA[ " & Customer.Customer_Mobile & "]]></cell>"
            ret = ret & "<cell><![CDATA[ " & Customer.Customer_FirstName & " " & Customer.Customer_LastName & "]]></cell>"
            ret = ret & "</row>"
        Next
        
        SQLDB.Dispose()
        CustList = Nothing
               
        ret = ret & "</rows>"
        cont.Response.ContentType = "text/xml"
        cont.Response.Write(ret)
        
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class